package es.udc.fbellas.j2ee.tiendaweb.http.view.actionforms;

import java.util.Collection;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import es.udc.fbellas.j2ee.util.struts.action.PropertyValidator;
import es.udc.fbellas.j2ee.util.struts.action.DefaultActionForm;
import es.udc.fbellas.j2ee.tiendaweb.http.view.applicationobjects.Countries;
import es.udc.fbellas.j2ee.tiendaweb.http.view.applicationobjects.Languages;

public class UserProfileForm extends DefaultActionForm {

    public final static String REGISTER_ACTION = "REGISTER";
    public final static String UPDATE_ACTION = "UPDATE";
    
    private final static Collection ACTION_TYPES = Arrays.asList(
        new String[] {REGISTER_ACTION, UPDATE_ACTION});

    private String action;
    private String loginName;
    private String password;
    private String retypePassword;
    private String firstName;
    private String surname;
    private String email;
    private long numTarjeta;
    private String language;
    private String country;
    private String calle;
    private int numero;
    private String puerta;
    private int codPostal;
    private String fechaExp;
    private String nombreReceptor;
    
        
    
    private int idDir;
    
    
    public int getIdDir() {
		return idDir;
	}

	public void setIdDir(int idDir) {
		this.idDir = idDir;
	}

	
	
	
	
	
	
	public UserProfileForm() {
        reset();
    }
    
    public String getAction() {
        return action;
    }
    
    public void setAction(String action) {
        this.action = action;
    }
    
    public String getLoginName() {
        return loginName;
    }
    
    public void setLoginName(String loginName) {
        this.loginName = loginName.trim();
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String getRetypePassword() {
        return retypePassword;
    }
    
    public void setRetypePassword(String retypePassword) {
        this.retypePassword = retypePassword;
    }
    
    public String getFirstName() {
        return firstName;
    }
    
    public void setFirstName(String firstName) {
        this.firstName = firstName.trim();
    }
    
    public String getSurname() {
        return surname;
    }
    
    public void setSurname(String surname) {
        this.surname = surname.trim();
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email.trim();
    }
    
    public String getLanguage() {
        return language;
    }
    
    public void setLanguage(String language) {
        this.language = language;
    }
    
    public String getCountry() {
        return country;
    }
    
    public void setCountry(String country) {
        this.country = country;
    }

    public void reset(ActionMapping mapping, HttpServletRequest request) {
        reset();
    }

    public ActionErrors validate(ActionMapping mapping,
        HttpServletRequest request) {
        
        ActionErrors errors = new ActionErrors();
        
        PropertyValidator.validateString(errors, "action", action, true, 
            ACTION_TYPES);

        if (REGISTER_ACTION.equals(action)) {
            PropertyValidator.validateMandatory(errors, "loginName", loginName);
            boolean validatePassword = PropertyValidator.validateMandatory(
                errors, "password", password);
            validatePassword = validatePassword &&
                PropertyValidator.validateMandatory(errors, "retypePassword", 
                    retypePassword);
            if (validatePassword && !password.equals(retypePassword)) {
                errors.add("password", 
                    new ActionMessage("ErrorMessages.password.doNotMatch"));
            }
        }
            
        PropertyValidator.validateMandatory(errors, "firstName", firstName);
        PropertyValidator.validateMandatory(errors, "surname", surname);
        TiendaWebPropertyValidator.validateEmailAddress(errors, "email", 
            email);
        PropertyValidator.validateString(errors, "language", language, true, 
            Languages.getLanguageCodes());
        PropertyValidator.validateString(errors, "country", country, true, 
            Countries.getCountryCodes());

        return errors;
        
    }
    
    private void reset() {
        action = null;
        loginName = null;
        password = null;
        retypePassword = null;
        firstName = null;
        surname = null;
        email = null;
        numTarjeta = 0;
        language = null;
        country = null;
        
    
    }


	public long getNumTarjeta() {
		return numTarjeta;
	}

	public void setNumTarjeta(long numTarjeta) {
		this.numTarjeta = numTarjeta;
	}

	public String getCalle() {
		return calle;
	}

	public void setCalle(String calle) {
		this.calle = calle;
	}

	public int getCodPostal() {
		return codPostal;
	}

	public void setCodPostal(int codPostal) {
		this.codPostal = codPostal;
	}

	public String getFechaExp() {
		return fechaExp;
	}

	public void setFechaExp(String fechaExp) {
		this.fechaExp = fechaExp;
	}

	public int getNumero() {
		return numero;
	}

	public void setNumero(int numero) {
		this.numero = numero;
	}

	public String getPuerta() {
		return puerta;
	}

	public void setPuerta(String puerta) {
		this.puerta = puerta;
	}

	public String getNombreReceptor() {
		return nombreReceptor;
	}

	public void setNombreReceptor(String nombreReceptor) {
		this.nombreReceptor = nombreReceptor;
	}

}
